﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SEVN.Web.Framework.Modules.News
{
    public partial class CateNews : System.Web.UI.UserControl
    {
        WebFrameDataContext web = new WebFrameDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            DataList1.DataSource = web.Categories.Where(x => x.IsFontPage == true);
            DataList1.DataBind();

        }
        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            HiddenField hf = (HiddenField)e.Item.FindControl("hf1");
            Repeater rptLoadIsDefault = (Repeater)e.Item.FindControl("rpthome");
            if (hf != null && rptLoadIsDefault != null)
            {
                var list = from x in web.Newsletters
                           from y in web.Categories
                           orderby x.CreateDate descending
                           where x.CategoryID.ToString() == hf.Value && x.CategoryID == y.CategoryID
                           select new
                           {
                               x.NewsID,
                               x.Title,
                               SubTitle = x.SubTitle.Substring(0, 245),
                               Detail = x.Detail.Substring(0, 66),
                               x.Image,
                               x.ImageDefault,
                               x.CreateDate,
                               x.CategoryID
                           };

                rptLoadIsDefault.DataSource = list.Take(1);
                rptLoadIsDefault.DataBind();
            }
            Repeater rptRelateNews = (Repeater)e.Item.FindControl("RptRelateNews");
            if (hf != null && rptRelateNews != null)
            {
                var relatenew = from x in web.Categories
                                from y in web.Newsletters
                                orderby y.CreateDate descending
                                where x.CategoryID == y.CategoryID && x.CategoryID.ToString() == hf.Value
                                select new
                                {
                                    Title = y.Title.Substring(0, 86),
                                    y.NewsID,
                                    y.Image,
                                    y.CategoryID
                                };
                rptRelateNews.DataSource = relatenew.Skip(1).Take(3);
                rptRelateNews.DataBind();
            }
        }
    }
}